using AppFiniteStateMachine;
using UnityEngine;
using System;

namespace AppSoldierState
{
    public enum SoldierStateEnum
    {
        Init,
        Idle,
        Walk,
        Chase,
        Run,
        Death,
        Stop,
    }

    
    [Serializable]
    public class SoldierParams
    {
        public int attack = 3;
        public int health = 2;
        public string name = "Soldier_1";

        public GameObject soldier_obj;

        public float distance = 5f;
        public float direction = 1f;
        public float speed = 3f;

        [HideInInspector] public GameObject init_obj;
        [HideInInspector] public GameObject walk_obj;
        
        [HideInInspector]
        public Vector3 m_init_position;
        [HideInInspector]
        public int ordering;
    }

    
    public class SoldierStateManager : MonoBehaviour
    {
        public FiniteStateMachineBase fsm;
        public SoldierParams parameter;

        private void Awake()
        {
            fsm = new FiniteStateMachineBase(SoldierStateEnum.Init);
            
            // --- 这里初始化`state_dc`
            fsm.state_dc.Add(SoldierStateEnum.Init.ToString(), new SoldierInitState(this));
            fsm.state_dc.Add(SoldierStateEnum.Walk.ToString(), new SoldierWalkState(this));

            parameter.m_init_position = parameter.soldier_obj.transform.position;
            parameter.init_obj = BdTools.get_game_object_by_path(SoldierStateEnum.Init.ToString(), parameter.soldier_obj);
            parameter.walk_obj = BdTools.get_game_object_by_path(SoldierStateEnum.Walk.ToString(), parameter.soldier_obj);

            BdPackage.BdUtils.assert(parameter.soldier_obj.activeInHierarchy, "soldier_obj is not active in hierarchy");
        }

        void Start()
        {
            fsm.Start();
        }

        void Update()
        {
            fsm.Update();
        }

        public void TransitionState(object type)
        {
            fsm.TransitionState(type);
        }
    }
}